package io.github.shenbinglife.springboot2.layui.common.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeNode<ID> implements Tree {

    private ID id;
    private ID parentId;
    private String field;
    private boolean checked=false;
    private String name;
    private String icon;
    private String link;
    private Integer ordered;
    private List<TreeNode<ID>> children;
    private Boolean leaf;
    private Map<String, Object> attributes;

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    public ID getParentId() {
        return parentId;
    }

    public void setParentId(ID parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    @Override
    public List<TreeNode<ID>> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode<ID>> children) {
        this.children = children;
    }

    public void setChildrenWithLeafUpdate(List<TreeNode<ID>> children) {
        this.children = children;
        this.leaf = children == null || children.isEmpty();
    }

    public Boolean getLeaf() {
        return leaf;
    }

    public void setLeaf(Boolean leaf) {
        this.leaf = leaf;
    }

    public void addChild(TreeNode<ID> treeNode) {
        if (this.children == null) {
            this.children = new ArrayList<>();
        }
        this.children.add(treeNode);
        this.leaf = false;
    }

    public void addAttr(String name, String value) {
        if (this.attributes == null) {
            this.attributes = new HashMap<>();
        }
        this.attributes.put(name, value);
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
